const { trim } = require("jquery");

define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/order/index' + location.search,
                    add_url: 'order/order/add',
                    multi_url: 'order/order/multi',
                    import_url: 'order/order/import',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'sn', title: __('Sn'), operate: 'LIKE' },
                        {
                            field: '', title: __('商品名称'), operate: false, formatter: function (value, row, index) {
                                let name = ''
                                let order_type = row.order_type
                                for (let index = 0; index < row.goods.length; index++) {
                                    name += '商品名称:' + row.goods[index].goods_name + '<br>'
                                    if (order_type=='1') {
                                        name +='规格:' + row.goods[index].spec_array.spec_name + '<br>';
                                    }
                                }
                                return name.replace(/^(\s|,)+|(\s|,)+$/g, '');
                            }
                        },
                        { field: 'total_num', title: __('Total_num') },
                        { field: 'total_fee', title: __('Total_fee'), operate: 'BETWEEN' },
                        { field: 'pay_type', title: __('Pay_type'), searchList: { "1": __('Pay_type 1'), "2": __('Pay_type 2'), "3": __('Pay_type 3'), "4": __('Pay_type 4'), "5": __('Pay_type 5') }, formatter: Table.api.formatter.normal },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'paytime', title: __('Paytime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'contact', title: __('Contact'), operate: 'LIKE' },
                        { field: 'telephone', title: __('Telephone'), operate: 'LIKE' },
                        { field: 'address', title: __('Address'), operate: 'LIKE' },
                        { field: 'regio', title: __('区域'), operate: 'LIKE' },
                        { field: 'order_status', title: __('Order_status'), searchList: { "0": __('Order_status 0'), "10": __('Order_status 10'), "20": __('Order_status 20'), "30": __('Order_status 30'), "-1": __('Order_status -1') }, formatter: Table.api.formatter.status },
                        { field: 'express_com', title: __('Express_com'), operate: 'LIKE' },
                        { field: 'express_no', title: __('Express_no'), operate: 'LIKE' },
                        { field: 'sendtime', title: __('Sendtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'pay_fee', title: __('Pay_fee'), operate: 'BETWEEN' },
                        { field: 'user.username', title: __('User.username'), operate: 'LIKE' },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'select',
                                    title: '查看',
                                    text: '查看',
                                    extend: 'data-area=["90%","90%"]',
                                    classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                    url: 'order/order/edit',//接口地址 控制器名/方法名
                                },
                                {
                                    name: 'query',
                                    title: '物流',
                                    text: '物流',
                                    classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                    url: 'order/order/query?express_com={express_com}&express_no={express_no}',//接口地址 控制器名/方法名
                                    visible: function (row) {//判断显示隐藏 只有状态为待审核时展示操作按钮
                                        if (row.order_status >= '20') {
                                            return true;//显示
                                        } else {
                                            return false;//隐藏
                                        }
                                    },
                                },
                                {
                                    name: 'deliver',
                                    title: '发货',
                                    text: '发货',
                                    classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                    url: 'order/order/deliver',//接口地址 控制器名/方法名
                                    visible: function (row) {//判断显示隐藏 只有状态为待审核时展示操作按钮
                                        if (row.order_status == '10' || row.order_status == '20') {
                                            if (row.pay_type == '4') {
                                                if (row.examine == '20') {
                                                    return true;//显示
                                                } else {
                                                    return false;//显示
                                                }
                                            } else {
                                                return true;//显示
                                            }
                                        } else {
                                            return false;//隐藏
                                        }
                                    },
                                },
                                {
                                    name: 'examine',
                                    title: '审核',
                                    text: '审核',
                                    classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                    url: 'order/order/examine',//接口地址 控制器名/方法名
                                    visible: function (row) {//判断显示隐藏 只有状态为待审核时展示操作按钮
                                        if (row.order_status == '10' && row.pay_type == '4' && row.examine == '10') {
                                            return true;//显示
                                        } else {
                                            return false;//隐藏
                                        }
                                    },
                                },
                                {
                                    name: 'select',
                                    title: '删除',
                                    text: '',
                                    icon: 'fa fa-trash',
                                    extend: 'data-area=["90%","90%"]',
                                    classname: 'btn btn-primary btn-xs btn-view btn-ajax',
                                    confirm: '确定删除此项?',
                                    refresh: true,
                                    url: 'order/order/del',//接口地址 控制器名/方法名
                                    visible: function (row) {//判断显示隐藏 只有状态为待审核时展示操作按钮
                                        if (row.order_status > '0') {
                                            return false;//隐藏
                                        } else {
                                            return true;//显示
                                        }
                                    },
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        direct_thrust_performance: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/order/direct_thrust_performance' + location.search,
                    multi_url: 'order/order/multi',
                    import_url: 'order/order/import',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'order.sn', title: __('订单编号'), operate: 'LIKE' },
                        { field: 'goods_name', title: __('商品名称'), operate: 'LIKE' },
                        { field: 'image', title: __('商品图片'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'goods_num', title: __('商品数量'), operate: false },
                        { field: 'pay_fee', title: __('业绩'), operate: false },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        performance: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/order/performance' + location.search,
                    multi_url: 'order/order/multi',
                    import_url: 'order/order/import',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'order.sn', title: __('订单编号'), operate: 'LIKE' },
                        { field: 'goods_name', title: __('商品名称'), operate: 'LIKE' },
                        { field: 'image', title: __('商品图片'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'goods_num', title: __('商品数量'), operate: false },
                        { field: 'pay_fee', title: __('业绩'), operate: false },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        team_performance: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/order/team_performance' + location.search,
                    multi_url: 'order/order/multi',
                    import_url: 'order/order/import',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'order.sn', title: __('订单编号'), operate: 'LIKE' },
                        { field: 'goods.goods_name', title: __('商品名称'), operate: 'LIKE' },
                        { field: 'goods.image', title: __('商品图片'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'goods_num', title: __('商品数量'), operate: false },
                        { field: 'money', title: __('业绩'), operate: false },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'settletime', title: __('结算时间'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'is_settle', title: __('结算状态'), searchList: { 0: __('未结算'), 1: __('已结算') }, formatter: Table.api.formatter.normal },
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        series_performance: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/order/series_performance' + location.search,
                    multi_url: 'order/order/multi',
                    import_url: 'order/order/import',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'order.sn', title: __('订单编号'), operate: 'LIKE' },
                        { field: 'goods.goods_name', title: __('商品名称'), operate: 'LIKE' },
                        { field: 'goods.image', title: __('商品图片'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'goods_num', title: __('商品数量'), operate: false },
                        { field: 'money', title: __('业绩'), operate: false },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'settletime', title: __('结算时间'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'is_settle', title: __('结算状态'), searchList: { 0: __('未结算'), 1: __('已结算') }, formatter: Table.api.formatter.normal },
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        deliver: function () {
            Controller.api.bindevent();
        },
        examine: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
